<?php

declare(strict_types=1);

namespace App\Model\Arts;

use App\Master\Enum\PassportEnum;
use App\Model\Model;
use Hyperf\Cache\Annotation\Cacheable;

class UserModel extends Model
{
    /**
     * The table associated with the model.
     *
     * @var ?string
     */
    protected ?string $table = 'user';

    protected ?string $dateFormat = 'U';

    /**
     * 默认查询字段
     *
     * @var array|string[]
     */
    public array $select = [
        'id', 'phone', 'nickname', 'sex', 'head_img', 'created_at'
    ];

    /**
     * 中间件中获取用户信息
     * @param int $user_id
     * @return array
     */
    #[Cacheable(prefix: PassportEnum::USER_INFO, ttl: 7200, value: "#{user_id}", listener: PassportEnum::USER_INFO)]
    public function authUserInfo(int $user_id)
    {
        return (new UserModel())->getDetail(params: ['id' => $user_id], select: ['openid']);
    }
}
